package cn.iocoder.yudao.module.ad.controller.admin.cluelog;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.ad.controller.admin.cluelog.vo.AdClueLogPageReqVO;
import cn.iocoder.yudao.module.ad.controller.admin.cluelog.vo.AdClueLogRespVO;
import cn.iocoder.yudao.module.ad.controller.admin.cluelog.vo.AdClueLogSaveReqVO;
import cn.iocoder.yudao.module.ad.dal.dataobject.account.AdAccountDO;
import cn.iocoder.yudao.module.ad.dal.dataobject.channel.AdChannelDO;
import cn.iocoder.yudao.module.ad.dal.dataobject.cluelog.AdClueLogDO;
import cn.iocoder.yudao.module.ad.service.account.AdAccountService;
import cn.iocoder.yudao.module.ad.service.channel.AdChannelService;
import cn.iocoder.yudao.module.ad.service.cluelog.AdClueLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 广告线索记录")
@RestController
@RequestMapping("/ad/clue-log")
@Validated
public class AdClueLogController {

    @Resource
    private AdClueLogService clueLogService;

    @Resource
    private AdAccountService adAccountService;

    @Resource
    private AdChannelService adChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建广告线索记录")
    @PreAuthorize("@ss.hasPermission('ad:clue-log:create')")
    public CommonResult<Long> createClueLog(@Valid @RequestBody AdClueLogSaveReqVO createReqVO) {
        return success(clueLogService.createClueLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告线索记录")
    @PreAuthorize("@ss.hasPermission('ad:clue-log:update')")
    public CommonResult<Boolean> updateClueLog(@Valid @RequestBody AdClueLogSaveReqVO updateReqVO) {
        clueLogService.updateClueLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告线索记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ad:clue-log:delete')")
    public CommonResult<Boolean> deleteClueLog(@RequestParam("id") Long id) {
        clueLogService.deleteClueLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告线索记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('crm:clue:query','ad:clue-log:query')")
    public CommonResult<AdClueLogRespVO> getClueLog(@RequestParam("id") Long id) {
        AdClueLogDO clueLog = clueLogService.getClueLog(id);
        AdClueLogRespVO resp = BeanUtils.toBean(clueLog, AdClueLogRespVO.class);
        // clueLog 非空判断
        if (clueLog == null) {
            return success(null);
        }
        AdAccountDO account = adAccountService.getAccount(clueLog.getAccountId());
        Optional.ofNullable(account).ifPresent(data -> {
            resp.setAccountName(data.getName());
            AdChannelDO channel = adChannelService.getChannel(data.getChannelId());
            Optional.ofNullable(channel).ifPresent(data2 -> {
                resp.setChannelName(data2.getName());
            });
        });
        return success(resp);
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告线索记录分页")
    @PreAuthorize("@ss.hasPermission('ad:clue-log:query')")
    public CommonResult<PageResult<AdClueLogRespVO>> getClueLogPage(@Valid AdClueLogPageReqVO pageReqVO) {
        PageResult<AdClueLogDO> pageResult = clueLogService.getClueLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdClueLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告线索记录 Excel")
    @PreAuthorize("@ss.hasPermission('ad:clue-log:export')")
    @OperateLog(type = EXPORT)
    public void exportClueLogExcel(@Valid AdClueLogPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdClueLogDO> list = clueLogService.getClueLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告线索记录.xls", "数据", AdClueLogRespVO.class,
                BeanUtils.toBean(list, AdClueLogRespVO.class));
    }

}